<!DOCTYPE html>

<html>

<head>
  <title>Example 04.02 - Depth Material</title>
  <script type="text/javascript" src="../libs/three.js"></script>

  <script type="text/javascript" src="../libs/stats.js"></script>
  <script type="text/javascript" src="../libs/dat.gui.js"></script>
  <style>
    body {
      /* set margin to 0 and overflow to hidden, to go fullscreen */
      margin: 0;
      overflow: hidden;
    }
  </style>
</head>
<body>

<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="webgl">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    // once everything is loaded, we run our Three.js stuff.
    function init() {
        var scene = new THREE.Scene()

        scene.overrideMaterial = new THREE.MeshDepthMaterial()

        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 10, 130)
        camera.position.set(-50, 40, 50)
        camera.lookAt(scene.position)

        var renderer = new THREE.WebGLRenderer()
        renderer.setClearColor(0x000000, 1)
        renderer.setSize(window.innerWidth, window.innerHeight)

        document.getElementById('webgl').appendChild(renderer.domElement)

        var controls = new function () {
            this.cameraNear = camera.near
            this.cameraFar = camera.far
            this.rotationSpeed = 0.02
            this.numberOfObjects = scene.children.length

            this.removeCube = function () {
                var allChildren = scene.children
                var lastObject = allChildren[allChildren.length - 1]
                if (lastObject instanceof THREE.Mesh) {
                    scene.remove(lastObject)
                    this.numberOfObjects = scene.children.length
                }
            }

            this.addCube = function () {
                var cubeSize = Math.floor(3 + Math.random() * 5)
                var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize)
                var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff * Math.random()})
                var cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
                // position the cube randomly in the scene
                cube.position.x = -60 + Math.round((Math.random() * 100));
                cube.position.y = Math.round((Math.random() * 10));
                cube.position.z = -100 + Math.round((Math.random() * 150));
                scene.add(cube)
                this.numberOfObjects = scene.children.length
            }
        }
        var gui = new dat.GUI()
        gui.add(controls, 'rotationSpeed', 0, 0.5);
        gui.add(controls, 'addCube');
        gui.add(controls, 'removeCube');
        gui.add(controls, 'cameraNear', 0, 50).onChange(function (e) {
            camera.near = e;
        });
        gui.add(controls, 'cameraFar', 50, 200).onChange(function (e) {
            camera.far = e;
        });

        function render(){
            scene.traverse(function(child){
                if(child instanceof  THREE.Mesh){
                    child.rotation.x += controls.rotationSpeed
                    child.rotation.y += controls.rotationSpeed
                    child.rotation.z += controls.rotationSpeed
                }
            })
            requestAnimationFrame(render)
            renderer.render(scene, camera)
        }
        render()


    }

    window.onload = init
</script>
</body>
</html>
